package com.ht.right.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ht.right.entity.Button;
import com.ht.right.entity.Menu;
import com.ht.right.entity.MenuToBtn;
import com.ht.right.service.ButtonService;
import com.ht.right.service.MenuFacadeService;
import com.ht.right.service.MenuService;
import com.ht.right.service.MenuToBtnService;
import com.ht.right.service.RightService;
import com.ht.right.util.CommonUtil;

/**
 * 菜单外观模型服务层
 * 
 * @author 庞成祥
 * 
 */
public class MenuFacadeServiceImpl implements MenuFacadeService {
	private MenuService menuService;
	private ButtonService buttonService;
	private RightService rightService;
	private MenuToBtnService menuToBtnService;

	// ---加载获取权限过滤后的菜单-----
	@Override
	public List<Map<String, String>> findMenusData(String orgIds) {
		Set<String> set = rightService.getDocIds(orgIds, Menu.class.getSimpleName());
		Collection<Menu> s = menuService.findMenusByOrgIds(set);
		return menuService.dataList(s);
	}

	@Override
	public void addMenu(Menu menu, String sameLevelNodeId, String rigthData, String btnIds) {
		//添加菜单
		String menuId = menuService.addMenu(menu, sameLevelNodeId);
		//添加菜单按钮
		if (CommonUtil.isNullOrEmpty(btnIds)) {
		} else {
			addBtnToMenu(menu,btnIds);
		}
		//添加添加菜单查看权限
		if (CommonUtil.isNullOrEmpty(rigthData)) {
		} else {
			if ("[]".equals(rigthData)) {
			} else {
				
				rightService.updateRight(menuId, Menu.class.getSimpleName(), rigthData);
			}
		}
	}

	public void addBtnToMenu(Menu menu, String btnIds) {
		// ------保存菜单按钮信息-------
		List<Button> btnList = buttonService.findAllButton();
		Map<String, Button> btnMap = new HashMap<String, Button>();
		for (Button btn : btnList) {
			btnMap.put(btn.getUuid(), btn);
		}
		String[] arrBtnIds = btnIds.split(",");
		Button btn = null;
		MenuToBtn menuToBtn = null;
		for (int i = 0; i < arrBtnIds.length; i++) {
			System.out.println(arrBtnIds[i]);
			btn = btnMap.get(arrBtnIds[i]);
			menuToBtn = new MenuToBtn();
			menuToBtn.setMenu(menu);
			if (CommonUtil.isNullOrEmpty(btn)) {
			} else {
				menuToBtn.setBtnId(btn.getUuid());
				menuToBtn.setBtnName(btn.getBtnName());
				menuToBtn.setBtnFunction(btn.getBtnFunction());
				menuToBtn.setBtnIcon(btn.getBtnIcon());
				menuToBtn.setBtnTitle(btn.getBtnTitle());
				menuToBtn.setBtnStyle(btn.getBtnStyle());
			}
			menuToBtn.setBtnIsLook(1);
			menuToBtnService.save(menuToBtn);
		}
	}

	@Override
	public void updateMenu(Menu menu, String sameLevelNodeId, String position, String rigthData,String btnIds) {
		String menuId = menuService.updateMenu(menu, sameLevelNodeId, position);
		//添加菜单按钮
		if (CommonUtil.isNullOrEmpty(btnIds)) {
		} else {
			addBtnToMenu(menu,btnIds);
		}
		if (CommonUtil.isNullOrEmpty(rigthData)) {
		} else {
			if ("[]".equals(rigthData)) {
			} else {
				rightService.updateRight(menuId, Menu.class.getSimpleName(), rigthData);
			}
		}
	}

	@Override
	public List<Menu> findRootMenuList() {
		// TODO Auto-generated method stub
		return menuService.findRootMenuList();
	}

	@Override
	public List<Map<String, String>> findChildMenuList(Menu menu) {
		// TODO Auto-generated method stub
		return menuService.dataList(menuService.findChildMenuList(menu));
	}

	@Override
	public List<Map<String, String>> findSubMenuList(Menu menu) {
		// TODO Auto-generated method stub
		return menuService.dataList(menuService.findSubMenuList(menu));
	}
	
	//菜单、按钮结构树
	@Override
	public List<Map<String, String>> findMenuTreeNodeList(Menu menu) {
		List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
		List<Menu> menuList = menuService.findSubMenuList(menu);
		dataList.addAll(menuService.dataList(menuList));
		StringBuffer sb = new StringBuffer();
		for(Menu m:menuList){
			sb.append(",");
			sb.append(m.getUuid());
		}
		if(sb.length()>0){
			List<MenuToBtn> menuToBtnList = menuToBtnService.findMenuBtnByMenuIds(sb.substring(1));
			dataList.addAll(menuToBtnService.dataList(menuToBtnList));
		}
 		return dataList;
	}
	
	@Override
	public List<Map<String, String>> findAllMenuJson() {
		// TODO Auto-generated method stub
		return menuService.dataList(menuService.findAllMenuJson());
	}

	@Override
	public Menu findMenuById(String menuId) {
		// TODO Auto-generated method stub
		Menu menu = menuService.findMenuById(menuId);
		menu.setMenuToBtn(new HashSet<MenuToBtn>(menuToBtnService.findBtnListByMenuId(menuId)));
		return menu;
	}
	
	//点击菜单时，根据权限过滤按钮
	@Override
	public Menu findMenuById(String menuId,String orgIds) {
		// TODO Auto-generated method stub
		Menu menu = menuService.findMenuById(menuId);
		Set<String> set = rightService.getDocIds(orgIds, MenuToBtn.class.getSimpleName());
		menu.setMenuToBtn(new HashSet<MenuToBtn>(menuToBtnService.findBtnListByMenuId(menuId,set)));
		return menu;
	}
	
	@Override
	public List<Map<String, String>> findMenusHasWF() {
		// TODO Auto-generated method stub
		return menuService.dataList(menuService.findMenusHasWF());
	}

	// --------------按钮-----------------

	@Override
	public void addButton(Button button) {
		buttonService.saveButton(button);
	}
	
	@Override
	public void addButton(Button button, String rightId) {
		buttonService.saveButton(button);
		try {
			Menu  menu = menuService.findMenuById(rightId);
			rightService.addRight(button.getUuid(),Button.class.getSimpleName(), menu.getDataRightJson());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	@Override
	public List<Map<String, String>> findAllButton() {
		return buttonService.dataList(buttonService.findAllButton());
	}

	// -----------------------------------------

	//-------菜单按钮开始------
	@Override
	public void updateMenuBtn(MenuToBtn menuToBtn,String rigthData){
		menuToBtnService.update(menuToBtn);
		if (CommonUtil.isNullOrEmpty(rigthData)) {
		} else {
			if ("[]".equals(rigthData)) {
			} else {
				rightService.updateRight(menuToBtn.getUuid(), MenuToBtn.class.getSimpleName(), rigthData);
			}
		}
	}
	
	@Override
	public MenuToBtn findMenuBtnById(String id){
		return menuToBtnService.findMenuBtnById(id);
	}
	
	@Override
	public void delMenuBtnById(String id){
		menuToBtnService.delMenuBtnById(id);
	}
	//-------菜单按钮结束------
	
	
	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public RightService getRightService() {
		return rightService;
	}

	public void setRightService(RightService rightService) {
		this.rightService = rightService;
	}

	public ButtonService getButtonService() {
		return buttonService;
	}

	public void setButtonService(ButtonService buttonService) {
		this.buttonService = buttonService;
	}

	public MenuToBtnService getMenuToBtnService() {
		return menuToBtnService;
	}

	public void setMenuToBtnService(MenuToBtnService menuToBtnService) {
		this.menuToBtnService = menuToBtnService;
	}


}
